public class MaxProduct2 {

    public int maxProduct1(String[] words) {
        int ans = 0;
        for (int i = 0; i < words.length - 1; i++) {
            for (int j = i + 1; j < words.length; j++) {
                if (isValid(words[i], words[j])) {
                    ans = Math.max(ans, words[i].length() * words[j].length());
                }
            }
        }
        return ans;
    }

    public boolean isValid(String s1, String s2) {
        int[] temp = new int[26];
        for (int i = 0; i < s1.length(); i++) {
            temp[s1.charAt(i) - 'a']++;
        }
        for (int i = 0; i < s2.length(); i++) {
            if (temp[s2.charAt(i) - 'a'] != 0) return false;
        }
        return true;
    }

    public int maxProduct(String[] words) {
        int m = words.length;
        int[] count = new int[m];
        for (int i = 0; i < m; i++) {
            int sum = 0;
            for (int j = 0; j < words[i].length(); j++) {
                int temp = words[i].charAt(j) - 'a';
                if (((sum>>temp) & 1) == 0) sum += 1<<temp;
            }
            count[i] = sum;
        }
        int ans = 0;
        for (int i = 0; i < m - 1; i++) {
            for (int j = i + 1; j < m; j++) {
                if ((count[i] & count[j]) == 0) ans = Math.max(ans
                        , words[i].length() * words[j].length());
            }
        }
        return ans;
    }
}
